<template>
  <div class="app-container">
    <div class="filter-container">
      <el-row>
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-edit" @click="handleEdit">
          编辑
        </el-button>
        <el-button v-waves class="filter-item" type="success" icon="el-icon-plus" @click="handleSave">
          保存
        </el-button>
      </el-row>
      <el-row>
        <el-select
          v-model="listQuery.channelId"
          filterable
          :filter-method="dataFilter"
          placeholder="选择渠道"
          style="width: 150px;"
          class="filter-item"
          @keyup.enter.native="handleFilter"
        >
          <el-option v-for="item in options" :key="item.channelId" :label="item.channel_name" :value="item.channelId" />
        </el-select>
        <el-date-picker
          v-model="times"
          class="filter-item"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 250px;"
          type="daterange"
          value-format="yyyy-MM-dd"
        />
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
          搜索
        </el-button>
      </el-row>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="dayData"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="时间" align="center" width="150">
        <template slot-scope="{row}">
          <span>{{ row.dates }}</span>
        </template>
      </el-table-column>
      <el-table-column label="注册页UV量" min-width="120" align="center">
        <template slot-scope="{row}">
          <el-input v-if="edit!==0" v-model="row.channelUvCount" />
          <span v-if="edit===0">{{ row.channelUvCount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备安装量" min-width="120" align="center">
        <template slot-scope="{row}">
          <el-input v-if="edit!=0" v-model="row.userDeviceCount" />
          <span v-if="edit==0">{{ row.userDeviceCount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户注册量" min-width="120" align="center">
        <template slot-scope="{row}">
          <el-input v-if="edit!=0" v-model="row.userRegCount" />
          <span v-if="edit==0">{{ row.userRegCount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="会员数量" min-width="120" align="center">
        <template slot-scope="{row}">
          <el-input v-if="edit!=0" v-model="row.userVipCount" />
          <span v-if="edit==0">{{ row.userVipCount }}</span>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>

<script>
import axios from 'axios'
import { channelDailyReport } from '@/api/channel'
import { getChannelIdAndNick } from '@/api/user'
import waves from '@/directive/waves'
export default {
  name: 'EidtData',
  directives: { waves },
  data() {
    return {
      edit: 0,
      tableData: [],
      dayData: [],
      total: 0,
      daytotal: 0,
      tableKey: 0,
      listQuery: {
        current: 1,
        size: 1000,
        startTime: '',
        endTime: ''
      },
      times: [],
      daytimes: {
        startTime: '',
        endTime: ''
      },
      dayId: 0,
      daydialogFormVisible: false,
      dialogPvVisible: false,
      optionsCopy: [],
      options: [],
      listLoading: false
    }
  },
  created() {
    this.getChannelIdAndNick()
  },
  methods: {
    // 渠道下拉
    async getChannelIdAndNick() {
      const res = await getChannelIdAndNick()
      res.data.data.forEach(element => {
        if (element.channel_name !== null) {
          this.optionsCopy.push(element)
          this.options.push(element)
        }
      })
    },
    dataFilter(val) {
      this.value = val
      if (val) { // val存在
        this.options = this.optionsCopy.filter((item) => {
          if (!!~item.channel_name.indexOf(val) || !!~item.channel_name.toUpperCase().indexOf(val.toUpperCase())) {
            return true
          }
        })
      } else { // val为空时，还原数组
        this.options = this.optionsCopy
      }
    },
    // 获取列表
    async channelDailyReport(listQuery) {
      const res = await channelDailyReport(this.listQuery)
      if (res.data.code === 200) {
        this.options.forEach(element => {
          if (element.channelId === this.listQuery.channelId) {
            this.listQuery.channelName = element.channel_name
          }
        })
        const data = {}
        data.start = this.listQuery.startTime
        data.end = this.listQuery.startTime
        data.channelName = this.listQuery.channelName
        data.data = JSON.stringify(res.data.data.records)
        axios.post('http://120.55.88.79:7000/api/put', data, { headers: { 'Content-Type': 'application/json' }}).then(res => {
          // console.log('res=>', res)
          this.dayData = res.data.data
        })
      } else {
        this.$notify({
          title: 'Fail',
          message: res.data.msg,
          type: 'fail',
          duration: 2000
        })
      }
    },
    // 搜索
    handleFilter() {
      this.listQuery.current = 1
      console.log(this.times)
      if (this.times === null) {
        this.listQuery.endTime = ''
        this.listQuery.startTime = ''
      } else if (this.times.length === 0) {
        this.listQuery.endTime = ''
        this.listQuery.startTime = ''
      } else {
        this.listQuery.endTime = this.times[1]
        this.listQuery.startTime = this.times[0]
      }
      this.channelDailyReport(this.listQuery)
    },
    // 开启编辑
    handleEdit() {
      this.edit = 1
    },
    // 保存编辑内容
    handleSave() {
      const data = {}
      data.start = this.listQuery.startTime
      data.end = this.listQuery.endTime
      data.channelName = this.listQuery.channelName
      data.data = JSON.stringify(this.dayData)
      const that = this
      axios.post('http://120.55.88.79:7000/api/save', data, { headers: { 'Content-Type': 'application/json' }}).then(res => {
        // console.log('res=>', res)
        that.dayData = res.data.data
        that.edit = 0
      })
    }
  }
}
</script>

<style scoped>

</style>
